package org.jeecg.flowable.controller.oa;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.flowable.entity.definition.BpmForm;
import org.jeecg.flowable.entity.oa.BpmOALeave;
import org.jeecg.flowable.service.oa.BpmOALeaveService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * OA 请假申请 Controller，用于演示自己存储数据，接入工作流的例子
 *
 * @author jason
 * @author 芋道源码
 */
@RestController
@RequestMapping("/bpm/oa/leave")
@Validated
public class BpmOALeaveController {

    @Resource
    private BpmOALeaveService leaveService;

    @PostMapping("/create")
    @AutoLog(value = "创建请求申请")
    @ApiOperation(value="创建请求申请", notes="创建请求申请")
    public Result<?> createLeave(@Valid @RequestBody BpmOALeave createReqVO) {
        leaveService.save(createReqVO);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return Result.ok(leaveService.createLeave(sysUser.getId(), createReqVO));
    }

    @GetMapping("/get")
    @AutoLog(value = "获得请假申请")
    @ApiOperation(value="获得请假申请", notes="获得请假申请")
    public Result<?> getLeave(@RequestParam("id") Long id) {
        BpmOALeave leave = leaveService.getById(id);
        return Result.ok(leave);
    }

    @GetMapping("/page")
    @AutoLog(value = "获得请假申请分页")
    @ApiOperation(value="获得请假申请分页", notes="获得请假申请分页")
    public Result<?> getLeavePage(BpmOALeave bpmOALeave,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
        QueryWrapper<BpmOALeave> queryWrapper = QueryGenerator.initQueryWrapper(bpmOALeave, req.getParameterMap());
        Page<BpmOALeave> page = new Page<BpmOALeave>(pageNo, pageSize);
        IPage<BpmOALeave> pageList = leaveService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

}
